
package com.pekall.music.service;

import java.io.IOException;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;

import com.pekall.music.model.Music;
import com.pekall.music.model.MusicPositionInfo;
import com.pekall.music.util.MusicOperate;
import com.pekall.music.util.Util;

public class MusicService extends Service implements IPlayService {

    private MusicOperate mMusicOperate;

    private List<Music> mListMusic;

    private boolean threadDisable;

    private MusicPositionInfo mMusicInfo;

    private MediaPlayer mMediaPlayer;

    private OnCompletionListener mediaCompletionListner;

    /**
     * 初始播放位置
     */
    private int mPlayMusicId = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        mMusicOperate = new MusicOperate();
        mListMusic = mMusicOperate.getAllMusic(this);

        mMusicInfo = new MusicPositionInfo();
        mMediaPlayer = new MediaPlayer();
        mediaPrepare();

        new Thread(new Runnable() {

            public void run() {
                while (!threadDisable) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                    }
                    mMusicInfo.id = mPlayMusicId;
                    if (mMediaPlayer.isPlaying())
                        mMusicInfo.position = mMediaPlayer.getCurrentPosition();
                }
            }
        }).start();

        mediaCompletionListner = new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                next();
            }
        };

    }

    private void mediaPrepare() {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setOnCompletionListener(mediaCompletionListner);
            mMediaPlayer.setDataSource(mListMusic.get(mPlayMusicId).path);
            mMediaPlayer.prepare();
        } catch (IllegalArgumentException e1) {
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        if(intent!=null){
        String action = intent.getAction();
        if (action.equals(Util.PLAY_ACTION)) {
            play();
        } else if (action.equals(Util.NEXT_ACTION)) {
            next();
        } else if (action.equals(Util.PREVIOUS_ACTION)) {
            previous();
        }
        }
    }

    private void play() {
        inite();
    }

    private void previous() {
        if (mPlayMusicId == 0) {
            mPlayMusicId = mListMusic.size() - 1;
        } else {
            mPlayMusicId--;
        }

        mediaPrepare();
        mMusicInfo.position = 0;
        mMediaPlayer.start();
    }

    private void next() {
        if (mPlayMusicId == mListMusic.size() - 1) {
            mPlayMusicId = 0;
        } else {
            mPlayMusicId++;
        }
        mediaPrepare();
        mMusicInfo.position = 0;
        mMediaPlayer.start();
    }

    public void inite() {

        if (mMediaPlayer.isPlaying()) {
            mMusicInfo.isPlaying = false;
            mMediaPlayer.pause();
        } else {
            mMusicInfo.isPlaying = true;
            mMediaPlayer.seekTo(mMusicInfo.position);
            mMediaPlayer.start();
        }
    }

    public String getDateByPosition(List<Music> c, int position) {
        return c.get(position).path;
    }

    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.release();
    }

    public MusicPositionInfo getMusicPlayingInfo() {
        return mMusicInfo;
    }

    /* ========================== 绑定service ====================== */
    private ServiceBinder serviceBinder = new ServiceBinder();

    public class ServiceBinder extends Binder implements IPlayService {

        public MusicPositionInfo getMusicPlayingInfo() {
            return mMusicInfo;
        }

    }

    public IBinder onBind(Intent intent) {
        return serviceBinder;
    }
}
